package com.lemon.smartsafety.ui.fragment.safe.worker;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.common.PanelRcvBaseDataI;
import com.lemon.smartsafety.entity.common.PanelRcvBaseListItem;
import com.lemon.smartsafety.entity.event.common.PanelEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.service.BaseHttpServices;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.result.address.CompanySafeTestMode;
import com.lemon.smartsafety.ui.adapter.common.PanelRcvBaseAdapter;
import com.lemon.smartsafety.ui.adapter.common.RcvViewType;
import com.lemon.smartsafety.ui.adapter.safe.data.ItemTag;
import com.lemon.smartsafetynew.ui.fragment.base.BaseLocalListFragment;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.app.UserTypeHelper;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Cooliean on 16/3/24.
 * 安全巡查【Worker 安全员】 主功能面板
 */
public class PanelFragment extends BaseLocalListFragment<PanelRcvBaseDataI> implements RecyclerTClickListener<PanelRcvBaseDataI<ItemTag>> {

    private CompanyInfo mCompanyInfo = null;


    private CompanySafeTestMode companySafeTestMode;

    private boolean isHold=false;

    public static PanelFragment newInstance() {
        return new PanelFragment();
    }

    private void initData() {
        PanelRcvBaseListItem dataHead1 = new PanelRcvBaseListItem();
        dataHead1.setPanelViewType(RcvViewType.PanelViewType.TYPE_SECTION_HEAD);
        mDataList.add(dataHead1);


        PanelRcvBaseListItem dataTitle = new PanelRcvBaseListItem();
        dataTitle.setPanelViewType(RcvViewType.PanelViewType.TYPE_TITLE);
        if (mCompanyInfo != null) {
            dataTitle.setTitle(StringUtils.nullValue(mCompanyInfo.getCompany_name()));
        }
        mDataList.add(dataTitle);


        PanelRcvBaseListItem dataHead2 = new PanelRcvBaseListItem();
        dataHead2.setPanelViewType(RcvViewType.PanelViewType.TYPE_SECTION_HEAD);
        mDataList.add(dataHead2);

        //用户类型
        PanelRcvBaseListItem itemCheck = new PanelRcvBaseListItem();
        itemCheck.setPanelViewType(RcvViewType.PanelViewType.TYPE_COMMON_ITEM);
        itemCheck.setTitle(getString(R.string.role_title));
        itemCheck.setValue(UserTypeHelper.getUserTypeName());
        itemCheck.setShowArrow(false);
        itemCheck.setDisable(true);
        itemCheck.setIcon(R.drawable.ic_item_role);
        mDataList.add(itemCheck);


        PanelRcvBaseListItem dataHead211 = new PanelRcvBaseListItem();
        dataHead211.setPanelViewType(RcvViewType.PanelViewType.TYPE_SECTION_HEAD);
        mDataList.add(dataHead211);


        //安全巡查标题
        PanelRcvBaseListItem<ItemTag> dataHead33 = new PanelRcvBaseListItem<ItemTag>();
        dataHead33.setPanelViewType(RcvViewType.PanelViewType.TYPE_SECTION_HEAD);
        dataHead33.setTitle(getString(R.string.safe_title));
        mDataList.add(dataHead33);

        //安全巡查
        PanelRcvBaseListItem<ItemTag> itemSafeInspection = new PanelRcvBaseListItem<ItemTag>();
        itemSafeInspection.setTag(ItemTag.TAG_SAFE_INSPECTION);
        itemSafeInspection.setPanelViewType(RcvViewType.PanelViewType.TYPE_COMMON_ITEM);
        itemSafeInspection.setTitle(getString(R.string.safe_title));
        itemSafeInspection.setShowArrow(true);
        itemSafeInspection.setIcon(R.drawable.ic_fire_check);
        mDataList.add(itemSafeInspection);


        //数据采集占位
        PanelRcvBaseListItem dataHead31 = new PanelRcvBaseListItem();
        dataHead31.setPanelViewType(RcvViewType.PanelViewType.TYPE_SECTION_HEAD);
        mDataList.add(dataHead31);


        //数据采集标题
        PanelRcvBaseListItem<ItemTag> dataHead3 = new PanelRcvBaseListItem<ItemTag>();
        dataHead3.setPanelViewType(RcvViewType.PanelViewType.TYPE_SECTION_HEAD);
        dataHead3.setTitle("其它");
        mDataList.add(dataHead3);


        //我的报修
        PanelRcvBaseListItem<ItemTag> itemRepair = new PanelRcvBaseListItem<ItemTag>();
        itemRepair.setTag(ItemTag.TAG_WORKER_REPAIR);
        itemRepair.setPanelViewType(RcvViewType.PanelViewType.TYPE_COMMON_ITEM);
        itemRepair.setTitle(getString(R.string.safe_worker_repair));
        itemRepair.setShowArrow(true);
        itemRepair.setIcon(R.drawable.ic_item_repair);
        mDataList.add(itemRepair);


        //专项整治上报
        PanelRcvBaseListItem<ItemTag> itemZXZZ = new PanelRcvBaseListItem<ItemTag>();
        itemZXZZ.setTag(ItemTag.TAG_WORKER_ZXZZ);
        itemZXZZ.setPanelViewType(RcvViewType.PanelViewType.TYPE_COMMON_ITEM);
        itemZXZZ.setTitle(getString(R.string.safe_worker_zxzz));
        itemZXZZ.setShowArrow(true);
        itemZXZZ.setIcon(R.drawable.ic_item_upload);
        mDataList.add(itemZXZZ);


        PanelRcvBaseListItem dataHeadEnd = new PanelRcvBaseListItem();
        dataHeadEnd.setPanelViewType(RcvViewType.PanelViewType.TYPE_SECTION_HEAD);
        mDataList.add(dataHeadEnd);
    }

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();
        if (getArguments() != null) {
            mCompanyInfo = (CompanyInfo) getArguments().getSerializable(BundleKey.COMPANY_INFO);
        }
    }

    @Override
    protected void initViews() {
        super.initViews();


        initData();
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }

        getSafeTestInfo();
    }

    @Override
    public boolean goRefreshOnCreate() {
        return false;
    }

    @Override
    public LemonRcvAdapter<PanelRcvBaseDataI> getLemonAdapter() {
        return new PanelRcvBaseAdapter(mDataList, this);
    }


    @Override
    public void onPullRefresh() {

    }


    @Override
    public void onItemClick(PanelRcvBaseDataI<ItemTag> data, int position) {
        PanelEvent<ItemTag> event = new PanelEvent<ItemTag>();
        event.setTag(PanelFragment.class.getName());
        event.setDataI(data);
        AppService.getBus().post(event);
    }


    //获取安全巡查的 灯状态
    private void getSafeTestInfo(){

        LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");
        params.put("company_code",mCompanyInfo.getCompany_code());
        BaseHttpServices baseHttpServices=new BaseHttpServices(getActivity());

        HttpOkBase.httpBasePost(params, baseHttpServices.mSharedPreferencesServer.getRequestUrl(UrlInfo.CHECK_SAFE_TEST), "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {

                if (httpClient) {
                    try {
                        companySafeTestMode=  new Gson().fromJson(result, CompanySafeTestMode.class);
                        if (!CommonUtil.objIsEmpty(companySafeTestMode)) {
                            if (!companySafeTestMode.isIsError()) {
                                if(CommonUtil.strIsEmpty(companySafeTestMode.getResult())){
                                    return;
                                }

                                for(int i=0;i<mDataList.size();i++){
                                    try {
                                        PanelRcvBaseListItem<ItemTag> itemSafeManager= (PanelRcvBaseListItem<ItemTag>) mDataList.get(i);

                                        if(!CommonUtil.objIsEmpty(itemSafeManager.getTag())) {
                                            if (itemSafeManager.getTag().equals(ItemTag.TAG_SAFE_INSPECTION)) {
                                                LoggerHelper.warn(getClass(), "安全巡查");
                                                itemSafeManager.setShowWarning(true);
                                                itemSafeManager.setStatus(Integer.valueOf(companySafeTestMode.getResult()));
                                                mDataList.set(i, itemSafeManager);
                                                if (mAdapter != null) {
                                                    mAdapter.notifyDataSetChanged();
                                                }
                                                break;
                                            }
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            } else {
                                LoggerHelper.error(getClass(),result);
                            }
                        }
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }



    @Override
    public void onResume() {
        super.onResume();
        if(isHold){
            getSafeTestInfo();
            isHold=false;
        }


    }

    @Override
    public void onPause() {
        super.onPause();
        isHold=true;
    }

    @Override
    public void onStop() {
        super.onStop();
        isHold=true;
    }


}
